package com.ttai.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.igexin.sdk.PushManager;
import com.ttai.R;
import com.ttai.presenter.activity.QrListPresenter;
import com.ttai.presenter.activity.QrLoginPresenter;
import com.ttai.ui.adapter.LoginIdRecycleViewAdapter;
import com.ttai.untils.Constant;
import com.ttai.untils.ItemClickSupport;
import com.ttai.untils.ShareCodeUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ChooseId extends Activity {
    @Bind(R.id.idacount)
    RecyclerView idacount;
    LoginIdRecycleViewAdapter loginIdRecycleViewAdapter;
    QrListPresenter qrListPresenter;
    QrLoginPresenter qrLoginPresenter;
    @Bind(R.id.tv_taiidtitle)
    TextView tvTaiidtitle;
    private String qrId;
    private String appId;
    private String realAppId;

    public String getRealAppId() {
        return realAppId;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.avtivity_taiid);
        ButterKnife.bind(this);
        tvTaiidtitle.setText("请选择要扫码登录的账号");
        qrListPresenter = new QrListPresenter(this);
        qrLoginPresenter = new QrLoginPresenter(this);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);

        Intent intent = getIntent();
        qrId = intent.getStringExtra("qrId");
        appId = intent.getStringExtra("appId");
        realAppId = String.valueOf(ShareCodeUtil.codeToId(appId));
        loginIdRecycleViewAdapter = new LoginIdRecycleViewAdapter(this);
        idacount.setAdapter(loginIdRecycleViewAdapter);
        idacount.setLayoutManager(new GridLayoutManager(this, 3));

//        loginIdPresenter.getData();
        qrListPresenter.storeIdAcount(Constant.getToken());

        ItemClickSupport.addTo(idacount).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {

            private String appName;
            private String acountlogo;

            @Override
            public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                appName = qrListPresenter.getIdList().get(position).getId_name();
                acountlogo = qrListPresenter.getIdList().get(position).getAcountnumberlogo();
                Toast.makeText(ChooseId.this, appName, Toast.LENGTH_SHORT).show();
                String token = Constant.getToken();
                String cid = PushManager.getInstance().getClientid(ChooseId.this);
                System.out.println("cid" + cid + " token:" + token + " logo:" + acountlogo);

                qrLoginPresenter.qrLogin(acountlogo, appId, token, qrId);
//                otpCreatePresenter.crateOTP(token,cid,acountlogo,"1");

            }
        });
    }

    public LoginIdRecycleViewAdapter getLoginIdRecycleViewAdapter() {
        return loginIdRecycleViewAdapter;
    }
}
